package org.study.redis.api.util.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.resps.ScanResult;

import java.util.List;
import java.util.Set;

/**
 * Redis 操作工具类：Set
 * 参考 {@link redis.clients.jedis.commands.SetCommands}
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class RedisSetUtils {
    private RedisSetUtils() {
        throw new AssertionError("不能实例化 RedisSetUtils 工具类");
    }

    public static long sadd(String key, String... members) {
        long sadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sadd = jedis.sadd(key, members);
        }
        return sadd;
    }

    public static long sadd(String key, List<String> members) {
        long sadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sadd = jedis.sadd(key, members.toArray(new String[members.size()]));
        }
        return sadd;
    }

    public static Set<String> smembers(String key) {
        Set<String> smembers = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            smembers = jedis.smembers(key);
        }
        return smembers;
    }

    public static long srem(String key, String... members) {
        long srem;
        try (Jedis jedis = SysJedisPool.getResource()) {
            srem = jedis.srem(key, members);
        }
        return srem;
    }

    public static long srem(String key, List<String> members) {
        long srem;
        try (Jedis jedis = SysJedisPool.getResource()) {
            srem = jedis.srem(key, members.toArray(new String[members.size()]));
        }
        return srem;
    }

    public static String spop(String key) {
        String spop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            spop = jedis.spop(key);
        }
        return spop;
    }

    public static Set<String> spop(String key, long count) {
        Set<String> spop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            spop = jedis.spop(key, count);
        }
        return spop;
    }

    public static long scard(String key) {
        long scard;
        try (Jedis jedis = SysJedisPool.getResource()) {
            scard = jedis.scard(key);
        }
        return scard;
    }

    public static boolean sismember(String key, String member) {
        boolean sismember;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sismember = jedis.sismember(key, member);
        }
        return sismember;
    }

    public static List<Boolean> smismember(String key, String... members) {
        List<Boolean> smismember = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            smismember = jedis.smismember(key, members);
        }
        return smismember;
    }

    public static List<Boolean> smismember(String key, List<String> members) {
        List<Boolean> smismember = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            smismember = jedis.smismember(key, members.toArray(new String[members.size()]));
        }
        return smismember;
    }

    public static String srandmember(String key) {
        String srandmember = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            srandmember = jedis.srandmember(key);
        }
        return srandmember;
    }

    public static List<String> srandmember(String key, int count) {
        List<String> srandmember = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            srandmember = jedis.srandmember(key, count);
        }
        return srandmember;
    }

    public static ScanResult<String> sscan(String key, String cursor) {
        ScanResult<String> sscan = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sscan = jedis.sscan(key, cursor, new ScanParams());
        }
        return sscan;
    }

    public static ScanResult<String> sscan(String key, String cursor, ScanParams params) {
        ScanResult<String> sscan = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sscan = jedis.sscan(key, cursor, params);
        }
        return sscan;
    }

    public static Set<String> sdiff(String... keys) {
        Set<String> sdiff = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sdiff = jedis.sdiff(keys);
        }
        return sdiff;
    }

    public static Set<String> sdiff(List<String> keys) {
        Set<String> sdiff = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sdiff = jedis.sdiff(keys.toArray(new String[keys.size()]));
        }
        return sdiff;
    }

    public static long sdiffstore(String dstkey, String... keys) {
        long sdiffstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sdiffstore = jedis.sdiffstore(dstkey, keys);
        }
        return sdiffstore;
    }

    public static long sdiffstore(String dstkey, List<String> keys) {
        long sdiffstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sdiffstore = jedis.sdiffstore(dstkey, keys.toArray(new String[keys.size()]));
        }
        return sdiffstore;
    }

    public static Set<String> sinter(String... keys) {
        Set<String> sinter = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sinter = jedis.sinter(keys);
        }
        return sinter;
    }

    public static Set<String> sinter(List<String> keys) {
        Set<String> sinter = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sinter = jedis.sinter(keys.toArray(new String[keys.size()]));
        }
        return sinter;
    }

    public static long sinterstore(String dstkey, String... keys) {
        long sinterstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sinterstore = jedis.sinterstore(dstkey, keys);
        }
        return sinterstore;
    }

    public static long sinterstore(String dstkey, List<String> keys) {
        long sinterstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sinterstore = jedis.sinterstore(dstkey, keys.toArray(new String[keys.size()]));
        }
        return sinterstore;
    }

    public static long sintercard(String... keys) {
        long sintercard;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sintercard = jedis.sintercard(keys);
        }
        return sintercard;
    }

    public static long sintercard(List<String> keys) {
        long sintercard;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sintercard = jedis.sintercard(keys.toArray(new String[keys.size()]));
        }
        return sintercard;
    }

    public static long sintercard(int limit, String... keys) {
        long sintercard;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sintercard = jedis.sintercard(limit, keys);
        }
        return sintercard;
    }

    public static long sintercard(int limit, List<String> keys) {
        long sintercard;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sintercard = jedis.sintercard(limit, keys.toArray(new String[keys.size()]));
        }
        return sintercard;
    }

    public static Set<String> sunion(String... keys) {
        Set<String> sunion = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sunion = jedis.sunion(keys);
        }
        return sunion;
    }

    public static Set<String> sunion(List<String> keys) {
        Set<String> sunion = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sunion = jedis.sunion(keys.toArray(new String[keys.size()]));
        }
        return sunion;
    }

    public static long sunionstore(String dstkey, String... keys) {
        long sunionstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sunionstore = jedis.sunionstore(dstkey, keys);
        }
        return sunionstore;
    }

    public static long sunionstore(String dstkey, List<String> keys) {
        long sunionstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            sunionstore = jedis.sunionstore(dstkey, keys.toArray(new String[keys.size()]));
        }
        return sunionstore;
    }

    public static long smove(String srckey, String dstkey, String member) {
        long smove;
        try (Jedis jedis = SysJedisPool.getResource()) {
            smove = jedis.smove(srckey, dstkey, member);
        }
        return smove;
    }

    /*public static void main(String[] args) {
        System.out.println("sadd：" + sadd("key:set", "张三", "李四"));
        System.out.println("smembers：" + smembers("key:set"));
        System.out.println("sadd：" + srem("key:set", "李四"));
        System.out.println("smembers：" + smembers("key:set"));
    }*/
}
